package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.odin.solution.contract.oms.ImportReq;
import com.homedo.odin.solution.contract.oms.ListDetailsResp;
import com.homedo.odin.solution.contract.oms.ListDownloadsResp;
import com.homedo.odin.solution.contract.oms.SolProductListResp;
import com.homedo.odin.solution.contract.oms.UpdateListStatusReq;
import com.homedo.odin.solution.service.bizservice.oms.SolProductListBizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zhaoxudong
 * @date: 2018/3/7 15:26
 * @Description:
 */
@Api(description = "配置清单列表", tags = {"OMS Solution Product List"},
        produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/sol-list")
public class SolProductListController extends BaseController<SolProductListBizService> {

    @ApiOperation(value = "查询清单列表", notes = "查询清单列表",
            response = SolProductListResp.class)
    @GetMapping
    public ResultData solList(@ApiParam(value = "当前页码", required = true) @RequestParam("pageNum") Integer pageNum,
            @ApiParam(value = "每页数量", required = true) @RequestParam("pageSize") Integer pageSize,
            @ApiParam(value = "方案名称", required = false) @RequestParam(value = "solName", required = false) String solName,
            @ApiParam(value = "清单状态 0：未上架 1：已上架 2:全部", required = true) @RequestParam("status") Integer status) {
        SolProductListResp resp = baseService.findSolList(pageNum, pageSize, solName, status);
        logger.info("查询清单列表返回结果:{}", resp.toJson());
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "更新清单上下架状态", notes = "更新清单上下架状态")
    @PutMapping("/status")
    public ResultData updateStatus(@RequestBody UpdateListStatusReq request) {
        baseService.updateListStatus(request);
        return ResultData.newInstance();
    }

    @ApiOperation(value = "查看清单详情", notes = "查看清单详情",
            response = ListDetailsResp.class)
    @GetMapping("/detail/{listId}")
    public ResultData listDetail(@PathVariable("listId") Integer listId,
            @ApiParam("当前页码") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        ListDetailsResp resp = baseService.findListDetailsByListId(listId, pageNum, pageSize);
        logger.info("查看清单详情返回结果：{}", resp.toJson());
        return ResultData.newInstance(resp);
    }


    @ApiOperation(value = "清单下载明细", notes = "清单下载明细", response = ListDownloadsResp.class)
    @GetMapping("/downloads/{listId}")
    public ResultData downloads(@ApiParam("清单id") @PathVariable("listId") Integer listId,
            @ApiParam("当前页码") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        ListDownloadsResp resp = baseService.findListDownloadsByListId(listId, pageNum, pageSize);
        logger.info("清单下载明细返回结果:{}", resp.toJson());
        return ResultData.newInstance(resp);
    }


    @ApiOperation(value = "导入清单", notes = "导入清单")
    @PostMapping
    public ResultData list(@ApiParam("导入清单请求") @RequestBody ImportReq request) {
        baseService.importList(request);
        return ResultData.newInstance();
    }

    @ApiModelProperty(value = "下载清单内容", notes = "下载清单内容")
    @GetMapping("/downloads/content/{useId}")
    public void downloadsDetails(@ApiParam("下载id") @PathVariable("useId") Integer useId,
            HttpServletResponse response) {
        baseService.downloadsDetails(useId, response);
    }
}
